//
//  XdkHand.m
//  XdkHand
//
//  Created by 湛亚磊 on 2024/5/31.
//

#import "XdkHand.h"

@interface XdkHand()
@property (nonatomic,strong) VPBleCentralManage *veepooSDKBleManager; //设备管理
@property (nonatomic,strong) NSMutableArray<VPPeripheralModel*> *deviceModels; //设备列表
@end

@implementation XdkHand
UNI_EXPORT_METHOD(@selector(testAsyncFunc:callback:))
UNI_EXPORT_METHOD(@selector(initSDK:callback:))
UNI_EXPORT_METHOD(@selector(scanDevice:callback:))
UNI_EXPORT_METHOD(@selector(stopScan:))
UNI_EXPORT_METHOD(@selector(connectToDevice:callback:))
UNI_EXPORT_METHOD(@selector(disconnectDevice))
UNI_EXPORT_METHOD(@selector(getDeviceBattery:))
UNI_EXPORT_METHOD(@selector(testHeartStart:callback:))
UNI_EXPORT_METHOD(@selector(testBloodStart:callback:))
UNI_EXPORT_METHOD(@selector(testOxygenStart:callback:))
UNI_EXPORT_METHOD(@selector(sleepDataWithDayNumber:callback:))
UNI_EXPORT_METHOD(@selector(stepDataWithDayNumber:callback:))
UNI_EXPORT_METHOD(@selector(basicDataWithDayNumber:callback:))
UNI_EXPORT_METHOD(@selector(startReadDeviceAllData:callback:))
UNI_EXPORT_METHOD(@selector(startReadDeviceRunningData:callback:))
UNI_EXPORT_METHOD(@selector(testECGStart:callback:))
/**
 测试方法
 */
- (void)testAsyncFunc:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback {
    if (callback) {
        // 第一个参数为回传给js端的数据，第二个参数为标识，表示该回调方法是否支持多次调用，如果原生端需要多次回调js端则第二个参数传 YES;
        callback(@"success",NO);
    }
}

/**
 初始化SDK
// */
- (void)initSDK:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    @try {
        self.veepooSDKBleManager = [VPBleCentralManage sharedBleManager];
        _deviceModels = [[NSMutableArray alloc]init];
        NSLog(@"CMD:0x%02lx",self.veepooSDKBleManager);
    } @catch (NSException *exception) {
        NSDictionary *dictionary = [NSDictionary dictionary];
        dictionary = @{@"result":@"sucess",@"data":exception,@"code":@"200",@"msg":@"----"};
        callback(dictionary,NO);
    } @finally {
        
    }
    
}

/**
 扫描设备
 */
-(void)scanDevice:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    NSDictionary *dictionary = [NSDictionary dictionary];
    
    [self prepareScan];
    if (self.veepooSDKBleManager) {
        if (callback) {
            dictionary = @{@"result":@"sucess",@"data":[NSNull null],@"code":@"200",@"msg":@"设备开始扫描"};
            callback(dictionary,YES);
            [self.veepooSDKBleManager veepooSDKStartScanDeviceAndReceiveScanningDevice:^(VPPeripheralModel *peripheralModel) {
                if (![self customContainsObject:[peripheralModel deviceName] Mac:[peripheralModel deviceAddress]]) {
                    [self.deviceModels addObject:peripheralModel];
                    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
                    //遍历过滤
                    
                    for (VPPeripheralModel *model in self.deviceModels) {
                        [dictionary setObject:model.peripheral forKey:@"peripheral"];
                        [dictionary setObject:model.deviceName forKey:@"deviceName"];
                        [dictionary setObject:model.deviceAddress forKey:@"deviceAddress"];
                        [dictionary setObject:model.RSSI forKey:@"RSSI"];
                    }
                    
                    NSDictionary *res_dictionary = [NSDictionary dictionary];
                    res_dictionary = @{@"result":@"sucess",@"data":dictionary,@"code":@"200",@"msg":@"设备扫描"};
                    callback(dictionary,YES);
                }
            }];
        }
    }else{
        dictionary = @{@"result":@"fail",@"data":[NSNull null],@"code":@"200",@"msg":@"请先初始化"};
        callback(dictionary,NO);
    }
    
}

//判断是否新增设备
-(BOOL)customContainsObject:(NSString *)name Mac:(NSString *)mac{
    
    NSArray *names = [self.deviceModels valueForKeyPath:@"deviceName"];
    NSArray *macs = [self.deviceModels valueForKeyPath:@"deviceAddress"];
    
    if ([macs containsObject:mac]) {
        return YES;
    }
    if ([names containsObject:name]) {
        return YES;
    }
    return NO;
}

#pragma mark - 准备扫描
-(void)prepareScan{
    [self.deviceModels removeAllObjects];
    [self.veepooSDKBleManager veepooSDKDisconnectDevice];
}


/**
 停止扫描
 */
-(void)stopScan:(UniModuleKeepAliveCallback)callback{
    [self.veepooSDKBleManager veepooSDKStopScanDevice];
    if (callback) {
        NSDictionary *dictionary = [NSDictionary dictionary];
        dictionary = @{@"result":@"sucess",@"data":[NSNull null],@"code":@"200",@"msg":@"设备停止扫描"};
        callback(dictionary,NO);
    }
}

/**
 连接设备
 */
-(void)connectToDevice:(NSDictionary*)options callback:(UniModuleKeepAliveCallback)callback{
    NSString* deviceName = options[@"deviceName"];
    NSString* deviceAddress = options[@"deviceAddress"];
    
    VPPeripheralModel * tempMode = NULL;
    for (VPPeripheralModel *model in self.deviceModels) {
        if([model.deviceName isEqualToString:deviceName]){
            tempMode = model;
            break;
        }
    }
    if (tempMode==NULL) {
        NSDictionary *dictionary = [NSDictionary dictionary];
        dictionary = @{@"result":@"sucess",@"data":@"未找到设备",@"code":@"200",@"msg":@"未找到该设备"};
        callback(dictionary,YES);
    }else{
        __block NSDictionary *dictionary = [NSDictionary dictionary];
        [self.veepooSDKBleManager veepooSDKConnectDevice:tempMode deviceConnectBlock:^(DeviceConnectState connectState) {
            NSLog(@"CMD:连接状态  %d",connectState);
            switch (connectState) {
                case BlePoweredOff:
                    dictionary = @{@"result":@"sucess",@"data":@"蓝牙没有打开",@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                    break;
                case BleConnecting:
                    dictionary = @{@"result":@"sucess",@"data":@"蓝牙连接中",@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                    break;
                case BleConnectSuccess:
                    dictionary = @{@"result":@"sucess",@"data":@"蓝牙连接成功",@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                    break;
                case BleConnectFailed:
                    dictionary = @{@"result":@"sucess",@"data":@"蓝牙连接失败",@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                    break;
                case BleVerifyPasswordSuccess:
                    dictionary = @{@"result":@"sucess",@"data":@"验证密码成功",@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                    break;
                case BleVerifyPasswordFailure:
                    dictionary = @{@"result":@"sucess",@"data":@"验证密码失败",@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                    break;
                default:
                    break;
            }
        }];
    }
    
}

/**
 断开设备连接
 */
-(void)disconnectDevice{
    [[VPBleCentralManage sharedBleManager] veepooSDKDisconnectDevice];
}

/**
 设备电量
 */
-(void)getDeviceBattery:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        if ([self.veepooSDKBleManager isConnected]) {
            [[VPPeripheralManage shareVPPeripheralManager] veepooSDKReadDeviceBatteryPower:^(NSUInteger batteryPower) {
                dictionary = @{@"result":@"sucess",@"data":@{@"battery":@(batteryPower)},@"code":@"200",@"msg":@"连接成功"};
                callback(dictionary,NO);
            }];
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}

/**
 获取心率
 */
-(void)testHeartStart:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        if ([self.veepooSDKBleManager isConnected]) {
            NSString* startTestHeart = options[@"startTestHeart"];
            if ([startTestHeart isEqualToString:@"0"]) {
                [[self.veepooSDKBleManager peripheralManage] veepooSDKTestHeartStart:YES testResult:^(VPTestHeartState testHeartState, NSUInteger heartValue) {
                    switch (testHeartState) {
                        case VPTestHeartStateStart:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(0),@"state":@"开始检测心率，还没有测出结果"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestHeartStateTesting:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(heartValue),@"state":@"正在检测心率，已经测出心率值"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestHeartStateNotWear:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(-1),@"state":@"佩戴检测没有通过，测试已经结束"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestHeartStateDeviceBusy:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(-1),@"state":@"设备正忙不能测试了，测试已经结束"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestHeartStateOver:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(-1),@"state":@"测试正常结束，人为结束"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        default:
                            break;
                    }
                    
                }];
            }else{
                [[self.veepooSDKBleManager peripheralManage] veepooSDKTestHeartStart:NO testResult:^(VPTestHeartState testHeartState, NSUInteger heartValue) {
                    dictionary = @{@"result":@"sucess",@"data":@"结束心率测量",@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                }];
            }
            
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}

/**
 获取血压
 */
-(void)testBloodStart:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        if ([self.veepooSDKBleManager isConnected]) {
            NSString* startTestHeart = options[@"startTestBlood"];
            if ([startTestHeart isEqualToString:@"0"]) {
                [[self.veepooSDKBleManager peripheralManage] veepooSDKTestBloodStart:YES testMode:0 testResult:^(VPTestBloodState testBloodState, NSUInteger testBloodProgress, NSUInteger highBlood, NSUInteger lowBlood) {
                    switch (testBloodState) {
                        case VPTestBloodStateTesting:
                            dictionary = @{@"result":@"sucess",@"data":@{@"highBlood":@(0),@"lowBlood":@(0),@"testBloodProgress":@(testBloodProgress),@"state":@"开始检测血压，还没有测出结果"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestBloodStateDeviceBusy:
                            dictionary = @{@"result":@"sucess",@"data":@{@"highBlood":@(0),@"lowBlood":@(0),@"testBloodProgress":@(0),@"state":@"设备忙，测试结束"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestBloodStateTestFail:
                            dictionary = @{@"result":@"sucess",@"data":@{@"highBlood":@(0),@"lowBlood":@(0),@"testBloodProgress":@(0),@"state":@"测试无效，即为测试失败"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestBloodStateTestInterrupt:
                            dictionary = @{@"result":@"sucess",@"data":@{@"highBlood":@(0),@"lowBlood":@(0),@"testBloodProgress":@(0),@"state":@"测试被人为结束"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestBloodStateComplete:
                            dictionary = @{@"result":@"sucess",@"data":@{@"highBlood":@(0),@"lowBlood":@(0),@"testBloodProgress":@(0),@"state":@"测试已经完成"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestBloodStateNoFunction:
                            dictionary = @{@"result":@"sucess",@"data":@{@"highBlood":@(0),@"lowBlood":@(0),@"testBloodProgress":@(0),@"state":@"设备没有血压功能"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                            
                        default:
                            break;
                    }
                }];
            }else{
                [[self.veepooSDKBleManager peripheralManage] veepooSDKTestBloodStart:NO testMode:0 testResult:^(VPTestBloodState testBloodState, NSUInteger testBloodProgress, NSUInteger highBlood, NSUInteger lowBlood) {
                    dictionary = @{@"result":@"sucess",@"data":@"结束血压测量",@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                }];
            }
            
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}

/**
 获取血氧
 */
-(void)testOxygenStart:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        if ([self.veepooSDKBleManager isConnected]) {
            NSString* startTestHeart = options[@"startTestOxygen"];
            if ([startTestHeart isEqualToString:@"0"]) {
                [[self.veepooSDKBleManager peripheralManage] veepooSDKTestOxygenStart:YES testResult:^(VPTestOxygenState testOxygenState, NSUInteger oxygenValue) {
                    switch (testOxygenState) {
                        case VPTestOxygenStateStart:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(0),@"state":@"开始检测血氧，还没有测出结果"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestOxygenStateTesting:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(oxygenValue),@"state":@"正在检测血氧，已经测出血氧值"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestOxygenStateNotWear:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(-1),@"state":@"佩戴检测没有通过，测试已经结束"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestOxygenStateDeviceBusy:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(-1),@"state":@"设备正忙不能测试了，测试已经结束"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                        case VPTestOxygenStateOver:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(-1),@"state":@"测试正常结束，人为结束"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestOxygenStateNoFunction:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(-1),@"state":@"设备没有血氧功能"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestOxygenStateCalibration:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(0),@"state":@"血氧校准中"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestOxygenStateCalibrationComplete:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(-1),@"state":@"血氧校准完成"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        case VPTestOxygenStateInvalid:
                            dictionary = @{@"result":@"sucess",@"data":@{@"heartValue":@(-1),@"state":@"血氧功能暂时不可用"},@"code":@"200",@"msg":@"连接成功"};
                            callback(dictionary,YES);
                            break;
                        default:
                            break;
                    }
                }];
            }else{
                [[self.veepooSDKBleManager peripheralManage] veepooSDKTestOxygenStart:NO testResult:^(VPTestOxygenState testOxygenState, NSUInteger oxygenValue) {
                    dictionary = @{@"result":@"sucess",@"data":@"结束血氧测量",@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                }];
            }
            
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}

/**
 获取睡眠数据
 */
- (void)sleepDataWithDayNumber:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        
        if ([self.veepooSDKBleManager isConnected]) {
            NSLog(@"CMD:%@",@"什么啊~~~~");
            [[VPPeripheralManage shareVPPeripheralManager] veepooSDK_readSleepDataWithDayNumber:0 result:^(NSArray *sleepOneDayArray) {
                NSLog(@"CMD:%@",@"什么啊~~222222~~");
                if ([sleepOneDayArray count]>0) {
                    NSLog(@"CMD:%@",@"什么啊~~333333~~");
                    dictionary = @{@"result":@"sucess",@"data":sleepOneDayArray,@"code":@"200",@"msg":@"获取成功"};
                }else{
                    NSLog(@"CMD:%@",@"什么啊~~44444~~");
                    dictionary = @{@"result":@"sucess",@"data":@[],@"code":@"200",@"msg":@"没有睡眠数据"};
                }
                
                callback(dictionary,YES);
            }];
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}

/**
 获取运动数据
 */
-(void) stepDataWithDayNumber:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        if ([self.veepooSDKBleManager isConnected]) {
            [[self.veepooSDKBleManager peripheralManage] veepooSDK_readStepDataWithDayNumber:0 result:^(NSDictionary *stepDict) {
                dictionary = @{@"result":@"sucess",@"data":stepDict,@"code":@"200",@"msg":@"获取成功"};
            }];
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}
/**
 读取基本数据
 */
-(void) basicDataWithDayNumber:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        if ([self.veepooSDKBleManager isConnected]) {
            [[self.veepooSDKBleManager peripheralManage] veepooSDK_readBasicDataWithDayNumber:0 maxPackage:10000 result:^(NSArray *oneDayBasicArray, NSInteger totalPackage, NSInteger currentReadPackage) {
                dictionary = @{@"result":@"sucess",@"data":@{@"oneDayBasicArray":oneDayBasicArray,@"totalPackage":@(totalPackage),@"currentReadPackage":@(currentReadPackage)},@"code":@"200",@"msg":@"获取成功"};
                callback(dictionary,YES);
            }];
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}

- (void) startReadDeviceAllData:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        if ([self.veepooSDKBleManager isConnected]) {
            [[self.veepooSDKBleManager peripheralManage] veepooSdkStartReadDeviceAllDataWithReadStateChangeBlock:^(VPReadDeviceBaseDataState readState, NSUInteger totalDay, NSUInteger currentReadDayNumber, NSUInteger readCurrentDayProgress) {
                if(readState==0){
                    dictionary = @{@"result":@"sucess",@"data":@{@"totalDay":@(0),@"currentReadDayNumber":@(0),@"readCurrentDayProgress":@(0),@"state":@"开始读取数据"},@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                }else if(readState==1){
                    dictionary = @{@"result":@"sucess",@"data":@{@"totalDay":@(totalDay),@"currentReadDayNumber":@(currentReadDayNumber),@"readCurrentDayProgress":@(readCurrentDayProgress),@"state":@"读取中"},@"code":@"200",@"msg":@"连接成功"};
                    callback(dictionary,YES);
                }
            }];
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}

-(void)startReadDeviceRunningData:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        if ([self.veepooSDKBleManager isConnected]) {
            [[self.veepooSDKBleManager peripheralManage] veepooSDKStartReadDeviceRunningData:^(VPReadDeviceBaseDataState readState, NSUInteger totalTimes, NSUInteger currentReadTimes, NSUInteger readCurrentTimesProgress) {
                switch (readState) {
                    case VPReadDeviceBaseDataStart:
                        dictionary = @{@"result":@"sucess",@"data":@{@"totalTimes":@(0),@"currentReadTimes":@(0),@"readCurrentTimesProgress":@(0),@"state":@"开始读取数据"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                    case VPReadDeviceBaseDataReading:
                        dictionary = @{@"result":@"sucess",@"data":@{@"totalTimes":@(totalTimes),@"currentReadTimes":@(currentReadTimes),@"readCurrentTimesProgress":@(readCurrentTimesProgress),@"state":@"读取中"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                    case VPReadDeviceBaseDataComplete:
                        dictionary = @{@"result":@"sucess",@"data":@{@"totalTimes":@(totalTimes),@"currentReadTimes":@(currentReadTimes),@"readCurrentTimesProgress":@(readCurrentTimesProgress),@"state":@"读取全部完成"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                        
                    default:
                        break;
                }
            }];
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}

-(void)testECGStart:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback{
    __block NSDictionary *dictionary = [NSDictionary dictionary];
    if (callback) {
        NSString* startTestECG = options[@"startTestECG"];
        if ([self.veepooSDKBleManager isConnected]) {
            [[self.veepooSDKBleManager peripheralManage] veepooSDKTestECGStart:YES testResult:^(VPTestECGState testECGState, NSUInteger testProgress, VPECGTestDataModel *testModel) {
                switch (testECGState) {
                    case VPTestECGStateStart:
                        dictionary = @{@"result":@"sucess",@"data":@{@"testProgress":@(0),@"testModel":@{},@"state":@"开始检测ECG，还没有测出结果"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                    case VPTestECGStateTesting:
                        dictionary = @{@"result":@"sucess",@"data":@{@"testProgress":@(testProgress),@"testModel":@{@"muHearts":testModel.muHearts,@"muHrvs":testModel.muHrvs,@"muRrs":testModel.muRrs,@"muResRates":testModel.muResRates,@"muQTs":testModel.muQTs},@"state":@"正在检测ECG，已经测出心率值"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                    case VPTestECGStateNotLead:
                        dictionary = @{@"result":@"sucess",@"data":@{@"testProgress":@(0),@"testModel":@{},@"state":@"导联脱落"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                    case VPTestECGStateDeviceBusy:
                        dictionary = @{@"result":@"sucess",@"data":@{@"testProgress":@(0),@"testModel":@{},@"state":@"设备正忙不能测试了，测试已经结束"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                    case VPTestECGStateOver:
                        dictionary = @{@"result":@"sucess",@"data":@{@"testProgress":@(0),@"testModel":testModel,@"state":@"测试正常结束，人为结束"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                    case VPTestECGStateFailure:
                        dictionary = @{@"result":@"sucess",@"data":@{@"testProgress":@(0),@"testModel":@{},@"state":@"测试失败"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                    case VPTestECGStateComplete:
                        
                        dictionary = @{@"result":@"sucess",@"data":@{@"testProgress":@(testProgress),@"testModel":@{@"type":testModel.type,@"ecgType":testModel.ecgType,@"date":testModel.date,@"testTime":testModel.testTime,@"duration":testModel.duration,@"mac":testModel.mac,@"state":testModel.state,@"aveHeart":testModel.aveHeart,@"aveHrv":testModel.aveHrv,@"aveResRate":testModel.aveResRate,@"aveQT":testModel.aveQT,@"filterSignals":testModel.filterSignals,@"hearts":testModel.hearts,@"hrvs":testModel.hrvs,@"rrs":testModel.rrs,@"resRates":testModel.resRates,@"qts":testModel.qts},@"state":@"测试已经完成"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                    case VPTestECGStateNoFunction:
                        dictionary = @{@"result":@"sucess",@"data":@{@"testProgress":@(0),@"testModel":@{},@"state":@"设备没有此功能"},@"code":@"200",@"msg":@"连接成功"};
                        callback(dictionary,YES);
                        break;
                        
                    default:
                        break;
                }
            }];
        }else{
            dictionary = @{@"result":@"sucess",@"data":@"设备未连接",@"code":@"200",@"msg":@"连接成功"};
            callback(dictionary,NO);
        }
    }
}



@end
